<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

    <script type="module">
        import {consoleprint} from "./modules/first.js"

        consoleprint("async await")

        let requestGiteeuser = (username) => {
            return axios.get(`https://gitee.com/api/v5/users/${username}`) // 是promise对象,异步
        };


        let CreatePromise = () => {
            let p = new Promise((resolve, reject) => {
                setTimeout(function () {
                    resolve("aaaa")
                },2000)
            });
            return p // 返回 promise 对象
        };


        Promise.race([requestGiteeuser("taojun319"),requestGiteeuser(""),CreatePromise()]).then(res=>{ // race 返回第一个成功或拒绝的promise,一般是失败快
            console.log("this is Promise.race res:",res)
        }).catch(err=>{
            console.log("this is Promise.race err:",err)
        });


        Promise.all([requestGiteeuser("taojun319"),requestGiteeuser("taojun319"),CreatePromise()]).then(res=>{ //all 遇到 错误停止
            console.log("this is Promise.all res:",res)
        }).catch(err=>{
            console.log("this is Promise.all err:",err)
        });


        consoleprint("try, async await 同步执行");

        async function getUserInfo(user) { // async 执行
            let username = await user
            let info = await requestGiteeuser(username).then(res => { // await 变同步 等待执行完成
                return res
            });
            console.log("this is getUserInfo",info) // todo 上面必须是同步,否则拿不到info
        }
        getUserInfo("taojun319");

        async function reqGiteeResult(username) { // 包装一层，等待结果返回
            let resstatus
            await requestGiteeuser(username).then(res => {
                // console.log(res.status)
                resstatus = res.status
                // return res
            }).catch(err=>{
                console.log(`reqGiteeResult,err:${err}`)
                resstatus = 400
            });
            return resstatus
        }

        async function ff() { // 异步变同步执行， reqGiteeResult 返回结果后在
            try {
                let r1 = await reqGiteeResult("taojun319");
                let r2 = await reqGiteeResult("");
                let r3 = await reqGiteeResult("taojun319");
                console.log(r1, r2, r3) // 200 400 200
            } catch (e) {
                console.log(e)
            }
        }
        ff()


        consoleprint("try, async await 异步执行");
        let set2 =(time)=>{
            setTimeout(function () {
                let date= new Date()
                console.log(`开始时间${date}this is set2 time:${time}`)
            },time)
        };

        async function ff1() {
            try{
                set2(2000); // 一起执行，返回时间一样
                set2(2000);
                set2(2000);

            }catch (e) {
                console.log("this is ff1 err:",e)
            }
        }
        ff1()

        consoleprint("try, async await 综合");
        async function ff2() {
            let [a,b,c]= await Promise.all([requestGiteeuser("taojun319"),requestGiteeuser("taojun319"),CreatePromise()])
            console.log("this ff2 result:",a,b,c)
        }
        ff2()
    </script>
</head>
<body>

</body>
</html>